package com.tjp.eight.ui.fragment.commentdetails;

import com.tjp.eight.AllRetrofit;
import com.tjp.eight.ResultModel;
import com.tjp.eight.base.BasePresenterImpl;
import com.tjp.eight.constant.UrlConstant;
import com.tjp.eight.net.NetCallBack;
import com.tjp.eight.net.SubscribeCallBack;
import com.tjp.eight.ui.fragment.comment.SendCommentResultModel;
import com.tjp.eight.ui.fragment.comment.ZanCommentResultModel;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/2/24.
 */
public class CommentDetailsPresenter extends BasePresenterImpl<CommentDetailsView> {
    private CommentDetailService service = AllRetrofit.getRetrofit(UrlConstant.BaseUrl).create(CommentDetailService.class);

    public CommentDetailsPresenter(CommentDetailsView view) {
        attachView(view);
    }

    public void loadData(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        addSubscription(service.loadData(requestBody), new SubscribeCallBack<>(new NetCallBack<CommentDetailResultModel>() {
            @Override
            public void onSuccess(CommentDetailResultModel model) {
                view.loadData(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void sendComment(String sendCommentJson) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), sendCommentJson);
        addSubscription(service.sendComment(requestBody), new SubscribeCallBack<>(new NetCallBack<SendCommentResultModel>() {
            @Override
            public void onSuccess(SendCommentResultModel model) {
                view.sendComment(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void zanCommentDetails(String zanJson) {
        RequestBody rq = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), zanJson);
        addSubscription(service.zanCDetail(rq), new SubscribeCallBack<>(new NetCallBack<ZanCommentResultModel>() {
            @Override
            public void onSuccess(ZanCommentResultModel model) {
                view.zanSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void loadMoreData(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        addSubscription(service.loadData(requestBody), new SubscribeCallBack<>(new NetCallBack<CommentDetailResultModel>() {
            @Override
            public void onSuccess(CommentDetailResultModel model) {
                view.loadMoreData(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
}
